
package com.contractmodule.servlet;
import com.companymodule.dao.TbCompanyDAO;
import com.contractmodule.bean.TbAccessories;
import com.contractmodule.bean.TbContract;
import com.contractmodule.dao.*;
import com.jspsmart.upload.SmartUpload;
import com.usersmodule.bean.TbUsers;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.List;

import util.*;

public class TbContractServlet extends HttpServlet{
	 SmartUpload su = new SmartUpload(); 
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	
        String method = request.getParameter("method")==null?"":request.getParameter("method");
       PrintWriter out =  response.getWriter();
        int userId=((TbUsers) request.getSession().getAttribute("Users")).getUserId(); 
        String tempPath=(request.getRealPath("/")+ "../../upload/").replace("\\", "/");
       
        if("add".equals(method)){
        /*------------------------------------------------------------*/
        
        System.out.println("code:"+su.getRequest().getParameter("contractCode"));
        boolean flag=true;
     try{
     su = new SmartUpload(); 
     su.setCharset("UTF-8");
     su.initialize(this.getServletConfig(),request,response); 
     su.setDeniedFilesList("exe,bat,jsp,htm,html,vbs,"); 
     su.upload(); 
     String contractCode=su.getRequest().getParameter("contractCode");
     String contractName=su.getRequest().getParameter("contractName");
     
     int contractType=Integer.parseInt(request.getParameter("contractType"));
     String contractContent=su.getRequest().getParameter("contractContent");
     String startDateSigned=su.getRequest().getParameter("startDateSigned");
     String endDateSigned=su.getRequest().getParameter("endDateSigned");
     int contractState=Integer.parseInt(su.getRequest().getParameter("contractState")==null?"0":su.getRequest().getParameter("contractState"));
     String contractPrice=su.getRequest().getParameter("contractPrice");
     int companyId=Integer.parseInt(su.getRequest().getParameter("companyId"));
     
     String companyContract = su.getRequest().getParameter("companyContract");
     String contractSignedTime=su.getRequest().getParameter("contractSignedTime");
     String ourCompanyUser=su.getRequest().getParameter("ourCompanyUser");
        /*------------------------------------------------------------*/
     if(new TbContractDAO().add(new TbContract(contractCode,contractName,contractType,contractContent,startDateSigned,endDateSigned,contractState,contractPrice,companyId,userId,0,companyContract,contractSignedTime,ourCompanyUser))){
         request.getSession().setAttribute("result", "添加成功");
         }else{            
         request.getSession().setAttribute("result", "添加失败");
         }
     for (int i=0;i<su.getFiles().getCount();i++) 
     { 
     	com.jspsmart.upload.SmartFile file = su.getFiles().getFile(i); 
        if (file.isMissing()) continue; 
        file.saveAs(tempPath+ file.getFileName(), su.SAVE_PHYSICAL); 
        if(!new TbAccessoriesDAO().add(new TbAccessories(contractCode,file.getFileName(),userId))) flag=false;
     } 

     }catch (Exception e) {
    	 e.printStackTrace();
	}
   
     goList(method,request, response);
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_Contract", "ContractId", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
            goList(method,request, response);
        }
        if("archive".equals(method)){
        	if(new TbContractDAO().archiveByContractCode(request.getParameter("contractCode"))){
        		 request.getSession().setAttribute("result", "归档成功");
            }else{            
            	 request.getSession().setAttribute("result", "归档失败");
            }
            goList(method,request, response);
        }
        if("edit".equals(method)){
            boolean flag=true;
         try{
        	 su = new SmartUpload(); 
             su.setCharset("UTF-8");
             su.initialize(this.getServletConfig(),request,response); 
         su.setDeniedFilesList("exe,bat,jsp,htm,html,vbs,"); 
         su.upload(); 
         String contractCode=su.getRequest().getParameter("contractCode");
         String contractName=su.getRequest().getParameter("contractName");
         
         int contractType=Integer.parseInt(su.getRequest().getParameter("contractType")==null?"0":su.getRequest().getParameter("contractType"));
         String contractContent=su.getRequest().getParameter("contractContent");
         String startDateSigned=su.getRequest().getParameter("startDateSigned");
         String endDateSigned=su.getRequest().getParameter("endDateSigned");
         int contractState=Integer.parseInt(su.getRequest().getParameter("contractState")==null?"0":su.getRequest().getParameter("contractState"));
         String contractPrice=su.getRequest().getParameter("contractPrice");
         int companyId=Integer.parseInt(su.getRequest().getParameter("companyId"));
         
         String companyContract = su.getRequest().getParameter("companyContract");
         String contractSignedTime=su.getRequest().getParameter("contractSignedTime");
         String ourCompanyUser=su.getRequest().getParameter("ourCompanyUser");
            /*------------------------------------------------------------*/
                
         if(new TbContractDAO().edit(new TbContract(contractCode,contractName,contractType,contractContent,startDateSigned,endDateSigned,contractState,contractPrice,companyId,userId,0,companyContract,contractSignedTime,ourCompanyUser))){
             request.getSession().setAttribute("result", "添加成功");
             }else{            
             request.getSession().setAttribute("result", "添加失败");
             }
         for (int i=0;i<su.getFiles().getCount();i++) 
         { 
         	com.jspsmart.upload.SmartFile file = su.getFiles().getFile(i); 
            if (file.isMissing()) continue; 
            file.saveAs(tempPath+ file.getFileName(), su.SAVE_PHYSICAL); 
            if(!new TbAccessoriesDAO().add(new TbAccessories(contractCode,file.getFileName(),userId))) flag=false;
         } 
         }catch (Exception e) {
        	 e.printStackTrace();
    	}
     	
         goList(method, request, response);
        }
        if ("list".equals(method))goList(method,request, response);
        if ("listById".equals(method)) {
        	request.getSession(true).setAttribute("tbCompanyALL", new TbCompanyDAO().getTbCompanyAll());
            request.getSession(true).setAttribute("tbContract",new TbContractDAO().getTbContractByContractId(Integer.parseInt(request.getParameter("contractId"))));
            request.getSession(true).setAttribute("tbAccessorList",new TbAccessoriesDAO().getAccessoriesList(request.getParameter("contractCode")));
            response.sendRedirect("ContractModule/TbContractOperate.jsp");
    	}
        if("showadd".equals(method)){
            request.getSession().removeAttribute("tbContract");
            request.getSession(true).setAttribute("contractType", request.getParameter("contractType"));
            request.getSession(true).setAttribute("tbCompanyALL", new TbCompanyDAO().getTbCompanyAll());
            response.sendRedirect("ContractModule/TbContractOperate.jsp");
        }
        if("contractInfo".equals(method)){//the info of the contract
        	request.getSession().setAttribute("tbContract", new TbContractDAO().getTbContractByContractCode(request.getParameter("contractCode")));
        	request.getSession(true).setAttribute("tbDispatchListList",new TbDispatchListDAO().getTbDispatchListByContractCode(request.getParameter("contractCode")) );
        	response.sendRedirect("ContractModule/TbContractInfo.jsp");
        }
        if("deleAccessById".equals(method)){
        	String accessId = request.getParameter("accessId");
        	 if(UtilDAO.delin("Tb_Accessories", "AccessoriesId", accessId)){            
                 request.getSession().setAttribute("result", "删除成功");
                 }else{            
                 request.getSession().setAttribute("result", "删除失败");
                 }
        	 List<TbAccessories> accessoriesList =  new TbAccessoriesDAO().getAccessoriesList(request.getParameter("contractCode"));
        	 String result = "";
        	 if(accessoriesList!=null&&accessoriesList.size()>0){
        		 request.getSession().setAttribute("tbAccessorList", accessoriesList);
        		 for(TbAccessories accessories:accessoriesList){
        			 result+="<a href=\"../../../upload/"+accessories.getAccessoriesFileName()+"\">"+accessories.getAccessoriesFileName()+"</a>";
        			 result+="<input type=\"button\"  value=\"删除\" onclick=\"deleAccess('"+accessories.getAccessoriesId()+"','"+accessories.getContractCode()+"','"+new TbContractDAO().getTbContractByContractCode(accessories.getContractCode()).getContractId()+"')\"/>";
        		 }
        	 }
            out.println(result);
        }
        //get the company by contractCode
        if("getCompanyBycontractCode".equals(method)){
        	TbContract contract = new TbContractDAO().getTbContractByContractCode(request.getParameter("contractCode"));
        	String result = "";
        	if(contract!=null){
        		result=contract.getTbCompany().getCompanyId()+":"+contract.getTbCompany().getCompanyName();
        	}
        	out.print(result);
        }
    }
    public void goList(String method,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	if("add".equals(method)||"edit".equals(method)){
    		request.getSession().setAttribute("contractType",request.getParameter("contractType"));
        	request.getSession().setAttribute("isArchive", 0);
        	request.getSession().setAttribute("tbContractList",new TbContractDAO().getTbContractByContractTypeAndIsArchive(Integer.parseInt(request.getParameter("contractType")),0));
    	}else{
    	request.getSession().setAttribute("contractType",request.getParameter("contractType"));
    	request.getSession().setAttribute("isArchive", request.getParameter("isArchive"));
    	request.getSession().setAttribute("tbContractList",new TbContractDAO().getTbContractByContractTypeAndIsArchive(Integer.parseInt(request.getParameter("contractType")),Integer.parseInt(request.getParameter("isArchive"))));
    	}
        response.sendRedirect("ContractModule/TbContractList.jsp");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }
}
